using Game.Const;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class HairDressingMachineItem : BaseItem
	{
		public enum State
		{
			Static = 0,
			MoveDown = 1,
			StayDown1 = 2,
			StayDown2 = 3,
			MoveUp = 4,
			Error = 5
		}

		private const float UP_POSITION = 5f;

		private const float DOWN_POSITION = 4.54f;

		private const float BACK_UP_POSITION = 2.6f;

		private const float BACK_DOWN_POSITION = 2.14f;

		private int hairId;

		private int minHairId;

		private int maxHairId;

		private State state;

		private Transform display;

		private Transform headCover;

		private SpriteRenderer frontHair;

		private SpriteRenderer backHair;

		private CharacterItem selectCharacter;

		private float stateTime;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			headCover = model.Find("HeadCover");
			headCover.Find("ConfirmBtn").gameObject.layer = 15;
			headCover.Find("ConfirmBtn").SetLocalZ(-1E-05f);
			headCover.localPosition = new Vector3(headCover.localPosition.x, headCover.localPosition.y, 2E-05f);
			display = model.Find("Display");
			display.Find("LeftBtn").gameObject.layer = 15;
			display.Find("LeftBtn").SetLocalZ(-1E-05f);
			display.Find("RightBtn").gameObject.layer = 15;
			display.Find("RightBtn").SetLocalZ(-1E-05f);
			display.Find("Head").SetLocalZ(-1E-05f);
			display.Find("Head/Face").SetLocalZ(-1E-05f);
			Transform transform = display.Find("Color");
			transform.SetLocalZ(1E-05f);
			SpriteRenderer component = transform.GetComponent<SpriteRenderer>();
			component.color = base.itemData.bodyColor;
			frontHair = display.Find("Head/FrontHair/Sprite").GetComponent<SpriteRenderer>();
			display.Find("Head/FrontHair").SetLocalZ(-1E-05f);
			backHair = display.Find("Head/BackHair/Sprite").GetComponent<SpriteRenderer>();
			display.Find("Head/BackHair").SetLocalZ(1E-05f);
			hairId = data.state;
			string[] array = base.itemData.scriptParams.Split(',');
			minHairId = int.Parse(array[0]);
			maxHairId = int.Parse(array[1]);
			if (hairId == 0)
			{
				hairId = minHairId;
			}
			SetHair(hairId);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (state == State.Static)
			{
				return;
			}
			stateTime += Time.deltaTime;
			if (state == State.MoveDown)
			{
				headCover.SetLocalY(Mathf.Lerp(5f, 4.54f, stateTime * 2f));
				backMask.SetLocalY(Mathf.Lerp(2.6f, 2.14f, stateTime * 2f));
				if ((double)stateTime >= 0.7)
				{
					stateTime = 0f;
					state = State.StayDown1;
					if (selectCharacter != null)
					{
						Singleton<AudioManager>.Instance.PlaySound("sound_hair_change");
					}
				}
			}
			else if (state == State.StayDown1)
			{
				if (stateTime > 0.65f)
				{
					stateTime = 0f;
					state = State.StayDown2;
					if (selectCharacter != null)
					{
						selectCharacter.SetHair(hairId);
						selectCharacter.SetColliderEnabled(true);
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_explode_1", selectCharacter.transform.position + new Vector3(0f, 2.8f, -0.001f), 2f);
						Singleton<EffectManager>.Instance.ShowEffect(PathConst.EFFECT + "efc_blink_1", selectCharacter.transform.position + new Vector3(0f, 2.8f, -0.001f), 3f);
					}
				}
			}
			else if (state == State.StayDown2)
			{
				if (stateTime > 1.6f)
				{
					stateTime = 0f;
					state = State.MoveUp;
				}
			}
			else if (state == State.MoveUp)
			{
				headCover.SetLocalY(Mathf.Lerp(4.54f, 5f, stateTime * 2f));
				backMask.SetLocalY(Mathf.Lerp(2.14f, 2.6f, stateTime * 2f));
				if ((double)stateTime >= 0.5)
				{
					stateTime = 0f;
					state = State.Static;
				}
			}
			else if (state == State.Error)
			{
				if (stateTime <= 0.2f)
				{
					headCover.SetLocalY(Mathf.Lerp(5f, 4.8f, stateTime * 5f));
					backMask.SetLocalY(Mathf.Lerp(2.6f, 2.3999999f, stateTime * 5f));
				}
				else if (stateTime <= 0.4f)
				{
					headCover.SetLocalY(Mathf.Lerp(4.8f, 5f, (stateTime - 0.2f) * 5f));
					backMask.SetLocalY(Mathf.Lerp(2.3999999f, 2.6f, (stateTime - 0.2f) * 5f));
				}
				else
				{
					state = State.Static;
				}
			}
		}

		private void SetHair(int id)
		{
			if (id < minHairId)
			{
				hairId = maxHairId;
			}
			else if (id > maxHairId)
			{
				hairId = minHairId;
			}
			else
			{
				hairId = id;
			}
			Transform transform = Singleton<AssetBundleManager>.Instance.characterResource.transform;
			Transform transform2 = transform.Find("Model/Body/Head/FrontHair/" + hairId);
			if (transform2 == null)
			{
				frontHair.sprite = null;
			}
			else
			{
				frontHair.transform.localPosition = transform2.localPosition;
				frontHair.sprite = transform2.GetComponent<SpriteRenderer>().sprite;
			}
			transform2 = transform.Find("Model/Body/Head/BackHair/" + hairId);
			if (transform2 == null)
			{
				backHair.sprite = null;
				return;
			}
			backHair.transform.localPosition = transform2.localPosition;
			backHair.sprite = transform2.GetComponent<SpriteRenderer>().sprite;
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (state != 0)
			{
				return;
			}
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (!(raycastHit2D.collider != null) || !(raycastHit2D.collider.transform.parent.parent == model))
			{
				return;
			}
			if (raycastHit2D.collider.transform.name.Equals("ConfirmBtn"))
			{
				for (int i = 0; i < childItems.Count; i++)
				{
					BaseItem baseItem = childItems[i];
					if (!baseItem.itemData.canSit)
					{
						continue;
					}
					for (int j = 0; j < baseItem.childItems.Count; j++)
					{
						BaseItem baseItem2 = baseItem.childItems[j];
						if (baseItem2.itemData.scriptType == 1)
						{
							selectCharacter = (CharacterItem)baseItem2;
							selectCharacter.SetColliderEnabled(false);
							state = State.MoveDown;
							stateTime = 0f;
							Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
							break;
						}
					}
				}
				if (state == State.Static)
				{
					state = State.Error;
					stateTime = 0f;
					Singleton<AudioManager>.Instance.PlaySound("sound_error");
				}
				SetBtnConfirmState(true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else if (raycastHit2D.collider.transform.name.Equals("LeftBtn"))
			{
				hairId--;
				SetHair(hairId);
				SetBtnLeftState(true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
			else if (raycastHit2D.collider.transform.name.Equals("RightBtn"))
			{
				hairId++;
				SetHair(hairId);
				SetBtnRightState(true);
				Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			SetBtnConfirmState(false);
			SetBtnLeftState(false);
			SetBtnRightState(false);
		}

		private void SetBtnLeftState(bool isDown)
		{
			display.Find("LeftBtn/Down").SetObjectActive(isDown);
			display.Find("LeftBtn/Up").SetObjectActive(!isDown);
		}

		private void SetBtnRightState(bool isDown)
		{
			display.Find("RightBtn/Down").SetObjectActive(isDown);
			display.Find("RightBtn/Up").SetObjectActive(!isDown);
		}

		private void SetBtnConfirmState(bool isDown)
		{
			headCover.Find("ConfirmBtn/Down").SetObjectActive(isDown);
			headCover.Find("ConfirmBtn/Up").SetObjectActive(!isDown);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = hairId;
		}
	}
}
